$(function () {
    $('#save-label-btn').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var dialog = $('#goods-dialog');
        var name1 = $("input[name='name1']").val();
        var name2 = $("input[name='name2']").val();
        var status = $("input[name='status']").val();
        var priority = $("input[name='priority']").val();

        // var type_id = document.getElementsByName("type_id")[0].value;
        var submitType = self.attr("data-type");
        var type_id = self.attr('data-id');

        console.log(type_id);
        var url = '/cms/mall/add_label/';

        zlajax.post({
            'url':url,
            'data':{
                'name1':name1,
                'name2':name2,
                'status':status,
                'priority':priority,
                'label_id':type_id,
            },
            'success':function (data) {
                dialog.modal('hide');
                if(data['code']==200){
                    window.location.reload()
                }else{
                   const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});


$(function () {
    $(".edit-label-btn").click(function (event) {
        var self = $(this);
        var dialog = $('#goods-dialog');
        dialog.modal('show');

        var tr = self.parent().parent();
        var name1 = tr.attr("data-name1");
        var name2 = tr.attr("data-name2");
        // var type_img = tr.attr("data-img");
        var priority = tr.attr("data-priority");

        var nameInput = dialog.find("input[name='name1']");
        var nameInput2 = dialog.find("input[name='name2']");
        var priorityInput = dialog.find("input[name='priority']");
        var savebtn = dialog.find("#save-label-btn");

        nameInput.val(name1);
        nameInput2.val(name2);
        priorityInput.val(priority);
        savebtn.attr("data-type",'update');
        savebtn.attr("data-id",tr.attr('data-id'));
    })
});


$(function () {
    $('.delete-label-btn').click(function (event) {
        var self = $(this);
        var tr = self.parent().parent();
        var banner_id = tr.attr('data-id');
        zlalert.alertConfirm({
            'msg':'您确定要删除这个商品标签吗？',
            'confirmCallback':function () {
                zlajax.post({
                    'url':'/cms/mall/del_label/',
                    'data':{
                        'label_id':banner_id
                    },
                    'success':function (data) {
                        if(data['code']==200){
                            window.location.reload()
                        }else{
                            const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                        }
                    }
                })
            }
        })
    })
});

// 商品或类型展示在其他页面
$(function () {
    $('.to_show').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var tr = self.parent().parent();
        var show_id = tr.attr('data-id');

        zlajax.post({
            'url': '/cms/mall/goods_to_type/',
            'data':{
                'show_id': show_id,
                'status': 0,
                'is_type': 1,
            },
            'success':function (data) {
                if(data['code']==200){
                    // zlalert.alertSuccessToast('操作成功');
                    window.location.reload()
                }else{
                    const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});

// 不展示
$(function () {
    $('.not_show').click(function (event) {
        event.preventDefault();
        var self = $(this);
        var tr = self.parent().parent();
        var show_id = tr.attr('data-id');

        zlajax.post({
            'url': '/cms/mall/goods_to_type/',
            'data':{
                'show_id': show_id,
                'status': 0,
                'is_type': 0,
            },
            'success':function (data) {
                if(data['code']==200){
                    // zlalert.alertSuccessToast('操作成功');
                    window.location.reload()
                }else{
                    const timer =setInterval(() => {
                                  zlalert.alertError(data['message']);
                                  clearInterval(timer);

                                }, 100);
                }
            },
            'fail':function () {
                zlalert.alertNetworkError();
            }
        })
    })
});
